home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Imaging / G3 Minimum / Bounce 3-D
Encoding:
Text File  |  1999-03-04  |  5.6 KB  |  230 lines  |  [TEXT/ToyS]

  1. property kasMinSpeed : 2
  2. property kasMaxSpeed : 20
  3. property kasErase : true
  4. property kasName : "Bounce 3D V1.0"
  5.  
  6. global gasWinLoc -- Location of window
  7.  
  8. on run
  9.     pfLoad()
  10. end run
  11.  
  12.  
  13. on idle
  14.     set dims to random number from 250 to 500
  15.     set ballDim to random number from 40 to 200
  16.     set zLim to round (ballDim / 2) + 1
  17.     
  18.     set drawWin to ¬
  19.         display drawing titled ("Bounce 3-D V1.0 - shift=quit, w/opt to toggle trace") ¬
  20.             located at gasWinLoc ¬
  21.             with dimensions {dims, dims}
  22.     
  23.     set gasWinLoc to screen location of drawWin -- Save for comparison
  24.     
  25.     draw a box into drawWin ¬
  26.         inside of {0, 0, dims, dims} ¬
  27.         filling it by erasing it ¬
  28.         using state {bg col64:{0, 0, 0}}
  29.     
  30.     set rgn to CreateRgn(ballDim)
  31.     copy rgn to zrgn
  32.     set ball to CreateBall(ballDim, rgn)
  33.     
  34.     set bbox to {0, 0, ballDim, ballDim}
  35.     set sZ to random number from kasMinSpeed to kasMaxSpeed
  36.     set sY to random number from kasMinSpeed to kasMaxSpeed
  37.     set sX to random number from kasMinSpeed to kasMaxSpeed
  38.     
  39.     set userState to input state
  40.     
  41.     set i to 0
  42.     set z to 0
  43.     set erase to kasErase
  44.     
  45.     repeat until (shift key down of userState) or (i > 9999)
  46.         set item 1 of bbox to (item 1 of bbox) + sX
  47.         set item 3 of bbox to (item 3 of bbox) + sX
  48.         set item 2 of bbox to (item 2 of bbox) + sY
  49.         set item 4 of bbox to (item 4 of bbox) + sY
  50.         
  51.         -- Hit bottom? Hit top?
  52.         if (item 4 of bbox ≥ dims) then
  53.             set sY to -2 * (random number from kasMinSpeed to kasMaxSpeed)
  54.             set item 2 of bbox to dims - ballDim
  55.             set item 4 of bbox to dims
  56.         else if (item 2 of bbox < 0) then
  57.             set sY to round ((random number from kasMinSpeed to kasMaxSpeed) / 2)
  58.             set item 2 of bbox to 0
  59.             set item 4 of bbox to ballDim
  60.         end if
  61.         
  62.         -- Hit left? Hit right?
  63.         if (item 3 of bbox ≥ dims) then
  64.             set sX to -(random number from kasMinSpeed to kasMaxSpeed)
  65.             set item 1 of bbox to dims - ballDim
  66.             set item 3 of bbox to dims
  67.         else if (item 1 of bbox < 0) then
  68.             set sX to (random number from kasMinSpeed to kasMaxSpeed)
  69.             set item 1 of bbox to 0
  70.             set item 3 of bbox to ballDim
  71.         end if
  72.         
  73.         set z to z + sZ
  74.         
  75.         -- Hit front / back?
  76.         if (z ≥ zLim) then
  77.             set z to zLim - 1
  78.             set sZ to round ((random number from kasMinSpeed to kasMaxSpeed) / -5 - 0.5)
  79.         else if z ≤ 0 then
  80.             set sZ to round (0.5 + (random number from kasMinSpeed to kasMaxSpeed) / 5)
  81.         end if
  82.         
  83.         set zbox to {(item 1 of bbox) + z, (item 2 of bbox) + z, (item 3 of bbox) - z, (item 4 of bbox) - z}
  84.         
  85.         draw a picture into drawWin ¬
  86.             using data ball ¬
  87.             inside of zbox ¬
  88.             without recording
  89.         
  90.         if (erase) then
  91.             set ballRgn to calculate region from the mapping ¬
  92.                 of region zrgn inside of zbox
  93.             set diffRgn to calculate region from the difference ¬
  94.                 of region rgn with region ballRgn
  95.             
  96.             draw a region into drawWin ¬
  97.                 using data diffRgn ¬
  98.                 filling it by erasing it ¬
  99.                 without recording
  100.             
  101.             set rgn to ballRgn
  102.         end if
  103.         
  104.         set userState to input state
  105.         set i to i + 1
  106.         set sY to sY + 1 -- Gravity
  107.     end repeat
  108.     
  109.     set winLoc to screen location of ¬
  110.         (display drawing drawWin with disposal)
  111.     
  112.     if (winLoc is not gasWinLoc) then
  113.         set gasWinLoc to winLoc
  114.         pfSave()
  115.     end if
  116.     
  117.     if (option key down of userState) then
  118.         set kasErase to not kasErase
  119.         return 1
  120.     end if
  121.     
  122.     return 60
  123. end idle
  124.  
  125.  
  126. on CreateBall(dim, rgn)
  127.     set ballWin to display drawing titled ¬
  128.         "Creating Ball" with dimensions {dim, dim}
  129.     
  130.     set fgc to {} & ¬
  131.         {random number from 444 to 44444} & ¬
  132.         {random number from 444 to 44444} & ¬
  133.         {random number from 444 to 44444}
  134.     set box to {0, 0, dim, dim}
  135.     
  136.     set cnt to round (dim / 3)
  137.     
  138.     set colStepR to round ((65000 - (item 1 of fgc)) / cnt)
  139.     set colStepG to round ((65000 - (item 2 of fgc)) / cnt)
  140.     set colStepB to round ((65000 - (item 3 of fgc)) / cnt)
  141.     
  142.     draw a box into ballWin ¬
  143.         inside of box ¬
  144.         filling it by erasing it ¬
  145.         using state {bg color:"000000"}
  146.     
  147.     repeat with i from 1 to cnt
  148.         draw an oval into ballWin ¬
  149.             inside of box ¬
  150.             using state {fg col64:fgc, pen size:{2, 2}}
  151.         
  152.         set item 3 of box to (item 3 of box) - 1
  153.         set item 4 of box to (item 4 of box) - 1
  154.         
  155.         draw an oval into ballWin ¬
  156.             inside of box ¬
  157.             using state {fg col64:fgc, pen size:{1, 1}}
  158.         
  159.         set item 1 of box to (item 1 of box) + 1
  160.         set item 2 of box to (item 2 of box) + 1
  161.         set item 3 of box to (item 3 of box) - 1
  162.         set item 4 of box to (item 4 of box) - 1
  163.         
  164.         set item 1 of fgc to (item 1 of fgc) + colStepR
  165.         set item 2 of fgc to (item 2 of fgc) + colStepG
  166.         set item 3 of fgc to (item 3 of fgc) + colStepB
  167.     end repeat
  168.     
  169.     set ball to ¬
  170.         capture picture from ballWin ¬
  171.             inside region rgn ¬
  172.             using depth DepthOfMonitorContaining(screen location of ballWin) ¬
  173.             with pixel conversion and dithering
  174.     
  175.     display drawing ballWin with disposal
  176.     
  177.     return ball
  178. end CreateBall
  179.  
  180.  
  181. on DepthOfMonitorContaining(pt)
  182.     set displays to the display information
  183.     repeat with dsp in displays
  184.         if PtInRect(pt, monitor box of dsp) then ¬
  185.             return monitor depth of dsp
  186.     end repeat
  187.     return monitor depth of item 1 of displays
  188. end DepthOfMonitorContaining
  189.  
  190.  
  191. on PtInRect(pt, rc)
  192.     return (item 1 of pt ≥ item 1 of rc) ¬
  193.         and (item 2 of pt ≥ item 2 of rc) ¬
  194.         and (item 1 of pt ≤ item 3 of rc) ¬
  195.         and (item 2 of pt ≤ item 4 of rc)
  196. end PtInRect
  197.  
  198.  
  199. on CreateRgn(dim)
  200.     set ballWin to display drawing titled ¬
  201.         "Creating Region" with dimensions {dim, dim}
  202.     
  203.     set box to {0, 0, dim, dim}
  204.     
  205.     draw an oval into ballWin ¬
  206.         inside of box
  207.     
  208.     set rgn to capture region from ballWin
  209.     
  210.     display drawing ballWin with disposal
  211.     
  212.     return rgn
  213. end CreateRgn
  214.  
  215.  
  216. on pfLoad()
  217.     try
  218.         set myPref to load preference named kasName
  219.     on error
  220.         set myPref to {{-1, -1}}
  221.     end try
  222.     
  223.     set gasWinLoc to item 1 of myPref
  224. end pfLoad
  225.  
  226.  
  227. on pfSave()
  228.     save preference {gasWinLoc} named kasName
  229. end pfSave
  230.